package cn.hexcloud.dga.lineage.controller;

import cn.hexcloud.dga.lineage.bean.GovernanceLineageTable;
import cn.hexcloud.dga.lineage.service.GovernanceLineageTableService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.PUT;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiangdan7
 * @since 2023-09-06
 */
@RestController
@RequestMapping("/lineage")
public class GovernanceLineageTableController {

    private static final String RELATION_SOURCE = "source";
    private static final String RELATION_SINK = "sink";
    @Autowired
    GovernanceLineageTableService governanceLineageTableService;

    @GetMapping("/root/{table_name}")
    public String getRootTableName(@PathVariable("table_name") String tableName) {
        GovernanceLineageTable curTable = governanceLineageTableService.getOne(tableName);
        if (curTable == null) {
            return "none";
        }
        // 拼接JSON
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isRoot", true);
        jsonObject.put("comment", curTable.getComment());
        jsonObject.put("id", tableName);
        JSONArray childrenJsonArray = new JSONArray();
        // 对source表进行遍历
        getSourceJson(curTable, childrenJsonArray);
        // 对sink表进行遍历
        getSinkJson(curTable, childrenJsonArray);
        jsonObject.put("children", childrenJsonArray);
        return JSON.toJSONString(jsonObject);
    }

    @GetMapping("/child/{table_name}")
    public String getChildTableNameList(@PathVariable("table_name") String tableName, @RequestParam("relation") String relation) {
        // 有括号的表(有括号，是页面因为避免表名重复而增加的后缀，去掉括号部分
        if(tableName.contains("(")){
            tableName = tableName.substring(0,tableName.indexOf("("));
        }
        GovernanceLineageTable curTable = governanceLineageTableService.getOne(tableName);
        if (curTable == null) {
            return "none";
        }
        // 拼接JSON
        JSONArray childrenJsonArray = new JSONArray();
        if (RELATION_SOURCE.equals(relation)) {
            // 对source表进行遍历
            getSourceJson(curTable, childrenJsonArray);
        }
        if (RELATION_SINK.equals(relation)) {
            // 对sink表进行遍历
            getSinkJson(curTable, childrenJsonArray);
        }
        return JSON.toJSONString(childrenJsonArray);
    }
    private void getSinkJson(GovernanceLineageTable curTable, JSONArray childrenJsonArray) {
        if (curTable.getSinkTables() != null) {
            String[] sinkSplit = curTable.getSinkTables().split(",");
            for (String sinkTableName : sinkSplit) {
                GovernanceLineageTable sinkTable = governanceLineageTableService.getOne(sinkTableName);
                JSONObject sinkJsonObj = new JSONObject();
                sinkJsonObj.put("id", sinkTableName);
                sinkJsonObj.put("comment", sinkTable.getComment());
                sinkJsonObj.put("relation", "sink");
                childrenJsonArray.add(sinkJsonObj);
            }
        }
    }

    private void getSourceJson(GovernanceLineageTable curTable, JSONArray childrenJsonArray) {
        if (curTable.getSourceTables() != null) {
            String[] sourceSplit = curTable.getSourceTables().split(",");
            for (String sourceTableName : sourceSplit) {
                GovernanceLineageTable sourceTable = governanceLineageTableService.getOne(sourceTableName);
                JSONObject sourceJsonObj = new JSONObject();
                sourceJsonObj.put("id", sourceTableName);
                sourceJsonObj.put("comment", sourceTable.getComment());
                sourceJsonObj.put("relation", "source");
                childrenJsonArray.add(sourceJsonObj);
            }
        }
    }
}
